package com.mobile.check.activitys;

import com.clj.fastble.BleManager;
//import com.tencent.bugly.Bugly;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.droidlover.xdroidmvp.base.BaseApplication;
import cn.droidlover.xdroidmvp.kit.NToast;
import cn.droidlover.xdroidmvp.net.XApi;
import com.mobile.check.beans.BluetoothInfo;
import com.mobile.check.app.R;
import com.mobile.check.storage.CJsonStorage;
import com.mobile.check.storage.CKVStorage;
import com.mobile.check.bluetooth.BluetoothSppClient;
import com.mobile.check.net.MyNetProvider;
import com.mobile.check.util.AppContents;

public class GlobalPoolApplication extends BaseApplication {
    /**
     * 蓝牙SPP通信连接对象
     */
    public BluetoothSppClient mBSC = null;
    /**
     * 动态公共存储对象
     */
    public CKVStorage mDS = null;

    public static GlobalPoolApplication instance;

    protected  BluetoothInfo bluetoothInfo;

    /**
     * 未设限制的AsyncTask线程池(重要)
     */
    protected static ExecutorService FULL_TASK_EXECUTOR;

    static {
        FULL_TASK_EXECUTOR = (ExecutorService) Executors.newCachedThreadPool();
    }

    /**
     * 覆盖构造
     */
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        NToast.init(this);
        XApi.registerProvider(new MyNetProvider());
        this.mDS = new CJsonStorage(this, getString(R.string.app_name));
        BleManager.getInstance().init(this);
//        Bugly.init(getApplicationContext(), "9313f10930", true);

    }

    /**
     * 建立蓝牙连接
     *
     * @param sMac 蓝牙硬件地址
     * @return boolean
     */
    public boolean createConn(String sMac) {
        if (null == this.mBSC) {
            this.mBSC = new BluetoothSppClient(sMac);
            if (this.mBSC.createConn())
                return true;
            else {
                this.mBSC = null;
                return false;
            }
        } else
            return true;
    }

    /**
     * 关闭并释放连接
     *
     * @return void
     */
    public void closeConn() {
        if (null != this.mBSC) {
            this.mBSC.closeConn();
            this.mBSC = null;
        }
    }

    public boolean isConnect() {
        if (AppContents.ISTEST) {
           return true;
        }
        if (null != this.mBSC) {
            return mBSC.isConnect();
        } else {
            return false;
        }
    }
}
